'''
Created on Mar 21, 2011

@author: srikanth_nukala
'''

from com.sai.ParserVM import getPage
from com.sai.Settings import embedTag, stagingDir, proxyString, dataDir, \
    dbFileName
from elementtidy import TidyHTMLTreeBuilder
import datetime
import re
import sqlite3
import sys
import xml.dom.minidom


def iterateMovies(urlPrefix):
    print 'path is:'+dataDir+dbFileName
    
    conn = sqlite3.connect(dataDir+dbFileName)
    conn.row_factory = sqlite3.Row
    
    c = conn.cursor()
    insertCur = conn.cursor()
    
    #c.execute('''drop table movies''')
    
    # Create table
    #c.execute('''create table movies (uniquekey text NOT NULL , year numeric, title text, quality text, href text, actors text,unique(uniquekey))''')
    c.execute('''select * from movies where extract is null''')
    
    for row in c:
        movkey = row['uniquekey']
        linkUrl = urlPrefix + str(row['href'])
        print movkey+' and the url :'+linkUrl
        try:
            resultObjects = extractEmbedLinks(linkUrl)
        except:
            print 'error for:'+movkey
            next
        print resultObjects.keys()
        for linkName, markup in resultObjects.iteritems():
            print linkName+' val:'+markup
            insertCur.execute("insert into embedobjects values ('%s','%s','%s')" % (movkey,linkName,markup))
            print 'end of loop'
            conn.commit()
    
    # Save (commit) the changes
    #conn.commit()
    
    # We can also close the cursor if we are done with it
    c.close()
    insertCur.close()
    

def extractEmbedLinks(pageUrl):
    resultMap = {}
    #fetch the page
    response = getPage(pageUrl,proxyString)
    #sys.stdout.write('response is :'+str(response))
    fileLoc = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")
    print('filelocation is %s' % fileLoc)    
    sourceXml = open(stagingDir+fileLoc,'w')    
    sourceXml.write(response)
    sourceXml.close()
    tidyFileLoc = tidyContent(stagingDir+fileLoc)
    contentMap = extractEmbedMarkup(tidyFileLoc)
    if len(contentMap) < 1:
        print 'No content objects found: file len: '
        fileStr = open(stagingDir+fileLoc,'r').read().replace('\n','').replace('\r','')
        print len(fileStr)
        open(tidyFileLoc,'w').write(fileStr)
        print fileStr.find('megavideo')  
    else:
        print contentMap    
    return contentMap;

def getVideoLinks(fileLocation):
    return open(fileLocation).read().replace('\n','').replace('\r','');

def tidyContent(fileLocation):
    print('entering tidying def'+fileLocation)
    tidyData = TidyHTMLTreeBuilder.parse(fileLocation)
    tidyData.write(fileLocation+'.tidy', "utf-8")
    print fileLocation+'.tidy'
    return fileLocation+'.tidy' 

def extractEmbedMarkup(tidyFileLoc):
    tidyFile = open(tidyFileLoc,'r')
    print('entering embed'+tidyFileLoc)  
    embedContentMap = {}
    doc = ''
    try:
        doc = xml.dom.minidom.parse(tidyFile)    
        for tag in embedTag:
            for node in doc.getElementsByTagName(tag):
                print('found tag %s %s' % (tag,str(node.toxml())))
                hrefVal = str(node.getAttribute("src"))
                matches = re.search('\.(.*)\.com',hrefVal)            
                try:
                    print matches.group(1)            
                    siteName = str(matches.group(1))
                except Exception:
                    print 'exception extractEmbedMarkup'
                    siteName = tag           
                i = 2 #duplicate or multiple parts in same video hosting
                if len(str(node.toxml())) > 40 : #check for minimum markup length
                    if not embedContentMap.has_key(siteName):
                            embedContentMap[siteName] = str(node.toxml())
                    else:
                        embedContentMap[siteName+str(i)] = str(node.toxml())
                        i = i + 1
                else:
                    print 'Too little length for markup'
    except Exception as ex:
        print ex.args
        print 'exception in parse'
    return embedContentMap
